layui.use(['layer', 'form', 'jquery', 'table','upload'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var $ = layui.jquery;
    var table = layui.table;
    var upload = layui.upload;
    //获取使用人id
    var createId = localStorage.getItem("createId");

    //加载表格数据
    getTable();

    function getTable() {
        $.ajax({
            url: '/course/getCourse',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                tablereload(data.data);
            }
        });
    }

    var tableIns;

    //表格
    tablereload = function tablereload(data) {
        tableIns = table.render({
            elem: '#table',
            id: 'table',
            data: data,
            title: '个人课程列表',
            toolbar: '#toolbar',
            page: {
                limit: 10,
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: '首页',
                last: '尾页'
            },
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'courseId', title: '课程编号', align: 'center'},
                    {
                        field: 'url', title: '课程图片', align: 'center', templet: function (data) {
                            return "<img src='http://" + data.url + "' style='width: 100px;height: 100px'>";
                        }
                    },
                    {field: 'courseName', title: '课程名称', align: 'center'},
                    {
                        field: 'courseNprice', title: '课程价格', align: 'center', templet: function (data) {
                            return "￥" + data.courseNprice;
                        }
                    },
                    {
                        field: 'courseStatus', title: '上架/下架', align: 'center', templet: function (data) {
                            if (data.courseStatus == 2) {
                                return '<a href="javascript:updateStaus(' + data.courseId + ',' + data.courseStatus + ');"><input class="layui-input" type="checkbox" name="switch" lay-skin="switch" lay-text="上架|下架" checked/></a>'
                            } else {
                                return '<a href="javascript:updateStaus(' + data.courseId + ',' + data.courseStatus + ');"><input class="layui-input" type="checkbox" name="switch" lay-skin="switch" lay-text="上架|下架"/></a>'
                            }
                        }
                    },
                    {field: 'title', title: '课程分类', align: 'center'},
                    {field: 'courseCstatus', title: '审核状态', align: 'center',templet:function (data) {
                            if (data.courseCstatus == 3){
                                return "<p>未审核</p>";
                            }else if (data.courseCstatus == 4){
                                return "<p>审核通过</p><a href='javascript:detailed(" + data.courseId + ");' style='color: #2F4056'>审核详细</a>";
                            } else if (data.courseCstatus == 5){
                                return "<p>审核中</p>";
                            }else {
                                return "<p>审核未通过</p><a href='javascript:detailed("+data.courseId+");' style='color: #2F4056'>审核详细</a>";
                            }
                        }},
                    {
                        field: '', title: '操作', align: 'center', templet: function (data) {
                            return "<div>" +
                                "<a href='javascript:getCourseById(" + data.courseId + ");' style='color: #2F4056;margin-right: 10px'>查看</a>" +
                                "<a href='javascript:updateCourse(" + data.courseId + ");' style='color: #2F4056'>编辑</a><br/>" +
                                "<a href='javascript:getjournal("+data.courseId+");' style='color: #2F4056; margin-right: 10px'>日志</a>" +
                                "<a href='javascript:deleteCourse(\"" + data.courseId + "\",\""+data.courseName+"\");' style='color: #2F4056'>删除</a>" +
                                "</div>";
                        }
                    }
                ]
            ],
        });
    }
    //修改课程的上架/下架
    updateStaus = function updateStaus(id,status){
        if (status==2){
            status = 3;
        }else {
            status = 2;
        }
        $.ajax({
            url:'/course/updatecourseStatus',
            type:'post',
            dataType:'json',
            data:{'courseId':id,'courseStatus':status,'updateId':createId},
            success:function (data) {
                if (data.data){
                    getTable();
                } else{
                    return false;
                }
            }
        });
    }
    //搜索
    form.on('submit(search)', function (data) {
        $.ajax({
            url: '/course/getCourse',
            type: 'post',
            data: data.field,
            dataType: 'json',
            success: function (data) {
                tablereload(data.data);
            }
        });
    })
    //重置
    $('#reset').click(function () {
        $('input[name=courseName]').val('');
        $('input[name=courseNprice]').val('');
    });
    //查看课程详细信息
    getCourseById = function getCourseById(id) {
        $.ajax({
            url:'/course/getCourseById',
            type:'post',
            dataType:'json',
            data:{'courseId':id,'userId':createId},
            success:function (data) {
                viewDetails(data.data);
            }
        });
    }
    function viewDetails(data){
        layer.open({
            type: 2,
            title: '查看课程详细信息',
            shade: 0,
            area: ['1000px', '500px'],
            offset: 't',
            maxmin: true,
            btn: ['关闭'],
            content: '/course/course-see.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                var url = "http://"+data.url;
                obj.find('#url').attr("src",url);
                var courseUrl = "http://"+data.courseUrl;
                obj.find('#courseUrl').attr("src",courseUrl);
                obj.find('input[name=courseName]').val(data.courseName);
                obj.find('input[name=createDate]').val(data.createDate);
                obj.find('input[name=trueName]').val(data.trueName);
                obj.find('input[name=courseNprice]').val(data.courseNprice);
                obj.find('input[name=coursePrimary]').val(data.coursePrimary);
                obj.find('input[name=title]').val(data.title);
                obj.find('input[name=likeNum]').val(data.likeNum);
                obj.find('input[name=collectNum]').val(data.collectNum);
                obj.find('input[name=commentNum]').val(data.commentNum);
                obj.find('#courseIntro').val(data.courseIntro);
            }
        });
    }
    //审核详细
    detailed = function detailed(courseId) {
        localStorage.setItem('courseId',courseId);
        layer.open({
            type: 2,
            title: '审核详细',
            area: ['700px', '400px'],
            content: '/course/course-audit-details.html',
            shade: 0,
            maxmin: true,
            offset: 't',
            btn:['关闭'],
            success:function (layero,index) {

            }
        });
    }
    //修改课程信息
    updateCourse = function updateCourse(courseId) {
        //查看信息
        $.ajax({
            url:'/course/getCourseById',
            type:'post',
            dataType:'json',
            data:{'courseId':courseId,'userId':createId},
            success:function (data) {
                updateviewDetails(data.data);
            }
        });
    }
    function updateviewDetails(data){
        localStorage.setItem("classId",data.classId);
        layer.open({
            type: 2,
            title: '修改课程详细信息',
            shade: 0,
            area: ['650px', '500px'],
            offset: 't',
            maxmin: true,
            btn: ['修改','取消'],
            content: '/course/course-update.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                var url = "http://"+data.url;
                obj.find('#url').attr("src",url);
                obj.find('input[name=url]').val(data.url);
                var courseUrl = "http://"+data.courseUrl;
                obj.find('#courseUrl').attr("src",courseUrl);
                obj.find('input[name=courseUrl]').val(data.courseUrl);
                obj.find('input[name=courseTime]').val(data.courseTime);
                obj.find('input[name=courseName]').val(data.courseName);
                obj.find('input[name=createDate]').val(data.createDate);
                obj.find('input[name=trueName]').val(data.trueName);
                obj.find('input[name=courseNprice]').val(data.courseNprice);
                obj.find('input[name=coursePrice]').val(data.coursePrice);
                obj.find('input[name=coursePrimary]').val(data.coursePrimary);
                obj.find('#courseIntro').val(data.courseIntro);
                obj.find('input[name=courseId]').val(data.courseId);
                obj.find('input[name=userId]').val(data.userId);
                obj.find('input[name=courseStatus]').val(data.courseStatus);
                obj.find('input[name=courseCstatus]').val(data.courseCstatus);
            },
            yes:function (index,layero) {
                var obj = layero.find('iframe').contents();
                //获取form中的信息
                var course = obj.find('#form').serialize();
                $.ajax({
                    url: '/course/updateCourse',
                    type: 'post',
                    dataType: 'json',
                    data: course,
                    success: function (data) {
                        if (data.data){
                            layer.msg("修改成功");
                            getTable();
                        } else {
                            layer.msg("修改失败")
                        }

                    }
                });
                layer.close(index);
            }
        });
    }
    //日志
    getjournal = function getjournal(courseId) {
        localStorage.setItem('journalCourseId',courseId);
        layer.open({
            type: 2,
            title: '日志详细',
            area: ['700px', '400px'],
            content: '/course/course-journal.html',
            shade: 0,
            maxmin: true,
            offset: 't',
            btn:['关闭'],
            success:function (layero,index) {

            }
        });
    }
    //删除课程
    deleteCourse = function deleteCourse(courseId,courseName) {
        layer.confirm('是否删除' + courseName + '课程', {
            icon: 3, title: '提示'
        }, function (index) {
            //获取使用人id
            var createId = $('input[name=createId]').val();
            $.ajax({
                url: '/course/deleteCourse',
                type: 'post',
                dataType: 'json',
                data: {'courseId': courseId, 'userId': createId},
                success: function (data) {
                    if (data.data) {
                        //删除审核记录中的数据
                        $.ajax({
                            url: '/record/deleteRecord',
                            type: 'post',
                            dataType: 'json',
                            data: {'courseId': courseId, 'type': 2},
                            success: function (data) {
                                if (data.data) {
                                    layer.msg("删除成功");
                                    getTable();
                                } else {
                                    layer.msg("删除失败")
                                }
                            }
                        });
                    } else {
                        layer.msg("删除失败")
                    }
                }
            });
        })
    }
    //返回
    goBack = function goBack() {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    }
})
